package com.jifeng.br.pjj.bookreader.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jifeng.br.pjj.bookreader.entity.BookEntity;
import com.jifeng.br.pjj.bookreader.entity.BookLikeEntity;
import com.jifeng.br.pjj.bookreader.entity.CategoryEntity;
import com.jifeng.br.pjj.bookreader.entity.RecordEntity;
import com.jifeng.br.pjj.bookreader.service.*;
import com.jifeng.br.pjj.bookreader.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2023/7/18/018
 */
@RestController
@RequestMapping("/api/book")
public class BookApiController {
    @Autowired
    private BookService bookService;
    @Autowired
    private BookCatalogService catalogService;
    @Autowired
    private BookLikeService likeService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RecordService recordService;

    @GetMapping("/hot")
    public R hot() {
        return R.data(bookService.getHotList());
    }

    @GetMapping("/info/{bookId}")
    public R info(@PathVariable int bookId) {
        return R.data(bookService.getById(bookId));
    }

    @GetMapping("/{bookId}/catalog")
    public R catalog(@PathVariable int bookId) {
        return R.data(catalogService.getList(bookId));
    }

    @GetMapping("/like/check")
    public R checkLike(BookLikeEntity like) {
        return likeService.checkLike(like);
    }

    @GetMapping("/like/save")
    public R saveLike(BookLikeEntity like) {
        likeService.saveOrCancel(like);
        return R.ok();
    }

    @PostMapping("/read")
    public R read(@RequestBody RecordEntity record) {
        return catalogService.getCatalogByUser(record);
    }

    @GetMapping("/list")
    public R list(int page, int limit) {
        BookEntity book = new BookEntity();
        book.setStatus(1);
        return R.data(bookService.getPage(page, limit, book));
    }

    @GetMapping("/category")
    public R category() {
        return R.data(categoryService.list(new QueryWrapper<CategoryEntity>().eq("status", 1)));
    }

    @GetMapping("/category/{categoryId}")
    public R category(@PathVariable int categoryId, int page, int limit) {
        return R.data(bookService.getBookByCid(page, limit, categoryId));
    }

    @GetMapping("/record")
    public R record(int page, int limit, int userId) {
        return R.data(bookService.getBookByRecord(page, limit, userId));
    }

    @DeleteMapping("/record/delete")
    public R recordDelete(int bookId) {
        recordService.deleteRecord(bookId);
        return R.ok();
    }

    @GetMapping("/like")
    public R like(int page, int limit, int userId) {
        return R.data(bookService.getBookByLike(page, limit, userId));
    }
}
